//
//  LSGraffitiPaletteView.swift
//  Demo1
//
//  Created by Wanst on 2019/3/26.
//  Copyright © 2019 苏沫离. All rights reserved.
//

import UIKit


class LSGraffitiPaletteCell: UICollectionViewCell {
    //重写 isSelected 的 set 方法
    override var isSelected: Bool{
        didSet{
            if isSelected {
                self.layer.borderWidth = 2.0
                self.layer.borderColor = UIColor.white.cgColor
            }else{
                self.layer.borderWidth = 0.0
                self.layer.borderColor = UIColor.clear.cgColor
            }
        }
    }
}

class LSGraffitiPaletteView: UIView {
    let cellIdentifer = "LSGraffitiPaletteCell"
    var colorArray : [UIColor] = []
    var collectionView : UICollectionView!
    var coverButton : UIButton!
    
    //便利构造器
    convenience init() {
        self.init(frame: UIScreen.main.bounds)
        
        coverButton = UIButton.init(type: .custom)
        coverButton.backgroundColor = UIColor.black.withAlphaComponent(0.2)
        coverButton.frame = UIScreen.main.bounds
        coverButton.addTarget(self, action:#selector(dismissButtonClick) , for: .touchUpInside)
        self.addSubview(coverButton)
        
        
        var left : CGFloat = 16.0
        var width = UIScreen.main.bounds.width - left * 2
        var edgeInsets = UIEdgeInsets.init(top: 4, left: 4, bottom: 4, right: 4)
        var itemWidth = (width - edgeInsets.left - edgeInsets.right) / 12.0
        var height = itemWidth * 10.0 + edgeInsets.top + edgeInsets.bottom
        
        let layout = UICollectionViewFlowLayout.init()
        layout.itemSize = CGSize.init(width: itemWidth, height: itemWidth)
        layout.minimumInteritemSpacing = 0.0
        layout.minimumLineSpacing = 0.0
        layout.sectionInset = edgeInsets
        
        
        collectionView = UICollectionView.init(frame: CGRect.init(x: 16, y: 100, width: width, height: height), collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.white
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(LSGraffitiPaletteCell.self, forCellWithReuseIdentifier: cellIdentifer)
        collectionView.contentOffset = CGPoint.init(x: 0, y: 0)
        self.addSubview(collectionView)
        
        
        DispatchQueue.global(qos: .default).async {
            self.colorArray = LSGraffitiPaletteView.getColorArray()
            
            DispatchQueue.main.async {
                self.collectionView.reloadData()
            }
        }
        
    }
    
    func show() {
        self.collectionView.transform = CGAffineTransform.init(translationX: 0, y: self.collectionView.frame.height)
        UIApplication.shared.keyWindow?.addSubview(self)
        self.coverButton.alpha = 0
        
        weak var weakSelf = self
        UIView.animate(withDuration: 0.2) {
            self.coverButton.alpha = 1.0
            weakSelf?.collectionView.transform = CGAffineTransform.init(translationX: 0, y: 0)
        }
    }
    
    @objc func dismissButtonClick() {
        weak var weakSelf = self
        
        UIView.animate(withDuration: 0.2, animations: {
            weakSelf?.coverButton.alpha = 0
            weakSelf?.collectionView.transform = CGAffineTransform.init(translationX: 0, y: weakSelf?.collectionView.frame.height ?? 0)

        }) { (finished) in
            weakSelf?.coverButton.removeFromSuperview()
            weakSelf?.collectionView.removeFromSuperview()
            weakSelf?.removeFromSuperview()
        }
    }
}

/* 扩展：协议的实现
 */
extension LSGraffitiPaletteView : UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.colorArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifer, for: indexPath)
        cell.contentView.backgroundColor = self.colorArray[indexPath.row];
        return cell;
    }
}

/* 扩展：类方法
 */
extension LSGraffitiPaletteView {
    
    static func getColorArray() -> [UIColor] {
        
        var colorArray : [UIColor] = []
        
        colorArray.append(UIColor.init(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 235/255.0, green: 235/255.0, blue: 235/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 214/255.0, green: 214/255.0, blue: 214/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 194/255.0, green: 194/255.0, blue: 194/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 173/255.0, green: 173/255.0, blue: 173/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 153/255.0, green: 153/255.0, blue: 153/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 133/255.0, green: 133/255.0, blue: 133/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 112/255.0, green: 112/255.0, blue: 112/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 92/255.0, green: 92/255.0, blue: 92/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 71/255.0, green: 71/255.0, blue: 71/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0))
        
        colorArray.append(UIColor.init(red: 1/255.0, green: 55/255.0, blue: 74/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 1/255.0, green: 29/255.0, blue: 86/255.0, alpha: 1.0))
        colorArray.append(UIColor.init(red: 17/255.0, green:5/255.0, blue:59/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 46/255.0, green:6/255.0, blue:60/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 59/255.0, green:7/255.0, blue:27/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 92/255.0, green:7/255.0, blue:1/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 90/255.0, green:28/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 88/255.0, green:51/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 86/255.0, green:61/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 102/255.0, green:97/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 79/255.0, green:85/255.0, blue:4/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 38/255.0, green:62/255.0, blue:15/255.0, alpha:1.0))
        
        colorArray.append(UIColor.init(red: 0/255.0, green:77/255.0, blue:100/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 1/255.0, green:47/255.0, blue:123/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 26/255.0, green:10/255.0, blue:81/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 69/255.0, green:14/255.0, blue:88/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 85/255.0, green:16/255.0, blue:41/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 131/255.0, green:17/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 122/255.0, green:42/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 122/255.0, green:74/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 120/255.0, green:88/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 140/255.0, green:134/255.0, blue:2/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 111/255.0, green:118/255.0, blue:10/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 56/255.0, green:87/255.0, blue:26/255.0, alpha:1.0))
        
        colorArray.append(UIColor.init(red: 0/255.0, green:110/255.0, blue:142/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 0/255.0, green:66/255.0, blue:168/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 44/255.0, green:9/255.0, blue:119/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 97/255.0, green:24/255.0, blue:123/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 121/255.0, green:25/255.0, blue:61/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 181/255.0, green:26/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 173/255.0, green:61/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 169/255.0, green:104/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 165/255.0, green:123/255.0, blue:1/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 195/255.0, green:188/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 155/255.0, green:164/255.0, blue:15/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 78/255.0, green:122/255.0, blue:40/255.0, alpha:1.0))
        
        colorArray.append(UIColor.init(red: 0/255.0, green:140/255.0, blue:180/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 2/255.0, green:85/255.0, blue:214/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 55/255.0, green:26/255.0, blue:148/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 122/255.0, green:33/255.0, blue:158/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 152/255.0, green:36/255.0, blue:79/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 225/255.0, green:36/255.0, blue:1/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 218/255.0, green:81/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 210/255.0, green:130/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 209/255.0, green:157/255.0, blue:1/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 245/255.0, green:236/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 194/255.0, green:208/255.0, blue:23/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 102/255.0, green:156/255.0, blue:52/255.0, alpha:1.0))
        
        colorArray.append(UIColor.init(red: 1/255.0, green:160/255.0, blue:216/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 1/255.0, green:97/255.0, blue:253/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 76/255.0, green:34/255.0, blue:178/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 152/255.0, green:42/255.0, blue:187/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 185/255.0, green:45/255.0, blue:93/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:64/255.0, blue:20/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:106/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:170/255.0, blue:2/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 252/255.0, green:199/255.0, blue:0/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 253/255.0, green:251/255.0, blue:66/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 216/255.0, green:236/255.0, blue:55/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 117/255.0, green:187/255.0, blue:63/255.0, alpha:1.0))
        
        colorArray.append(UIColor.init(red: 3/255.0, green:199/255.0, blue:251/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 58/255.0, green:135/255.0, blue:253/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 94/255.0, green:48/255.0, blue:235/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 190/255.0, green:55/255.0, blue:242/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 230/255.0, green:59/255.0, blue:122/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:98/255.0, blue:79/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:134/255.0, blue:72/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:180/255.0, blue:63/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:203/255.0, blue:62/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:247/255.0, blue:106/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 228/255.0, green:238/255.0, blue:101/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 150/255.0, green:211/255.0, blue:95/255.0, alpha:1.0))
        
        colorArray.append(UIColor.init(red: 82/255.0, green:214/255.0, blue:251/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 116/255.0, green:166/255.0, blue:255/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 134/255.0, green:78/255.0, blue:253/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 211/255.0, green:87/255.0, blue:253/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 238/255.0, green:113/255.0, blue:158/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:140/255.0, blue:130/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:165/255.0, blue:125/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:199/255.0, blue:119/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:217/255.0, blue:119/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:249/255.0, blue:147/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 234/255.0, green:241/255.0, blue:143/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 177/255.0, green:220/255.0, blue:138/255.0, alpha:1.0))
        
        colorArray.append(UIColor.init(red: 147/255.0, green:226/255.0, blue:252/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 167/255.0, green:198/255.0, blue:255/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 177/255.0, green:140/255.0, blue:253/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 225/255.0, green:145/255.0, blue:253/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 244/255.0, green:163/255.0, blue:192/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:181/255.0, blue:175/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:197/255.0, blue:171/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:217/255.0, blue:168/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 253/255.0, green:227/255.0, blue:168/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:251/255.0, blue:185/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 241/255.0, green:246/255.0, blue:183/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 205/255.0, green:232/255.0, blue:180/255.0, alpha:1.0))
        
        colorArray.append(UIColor.init(red: 203/255.0, green:239/255.0, blue:255/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 210/255.0, green:225/255.0, blue:255/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 217/255.0, green:200/255.0, blue:254/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 239/255.0, green:202/255.0, blue:255/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 248/255.0, green:211/255.0, blue:223/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:219/255.0, blue:216/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 255/255.0, green:226/255.0, blue:213/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:235/255.0, blue:211/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 254/255.0, green:242/255.0, blue:213/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 253/255.0, green:252/255.0, blue:221/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 247/255.0, green:249/255.0, blue:219/255.0, alpha:1.0))
        colorArray.append(UIColor.init(red: 222/255.0, green:237/255.0, blue:212/255.0, alpha:1.0))
        
        return colorArray
    }
}
